package com.sgb.kjwl.view.ui.fragment.packers;

import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FrPackersWorkTeamBinding;
import com.sgb.kjwl.di.packers.DaggerPackersFragmentComponent;
import com.sgb.kjwl.di.packers.PackersFragmentModule;
import com.sgb.kjwl.model.entity.FindPackersEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.view.ui.activity.self.packers.PersonShopActivity;
import com.sgb.kjwl.view.ui.adapter.packers.FindPackersAdapter;
import com.sgb.kjwl.view.widget.PickPopWindow1;
import com.sgb.kjwl.viewmodel.packers.FragPackersWorkTeamModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.MultiLevelAreaPopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class PackersWorkTeamFragment extends AppLazyFragment {

    @Inject
    FragPackersWorkTeamModel viewModel;

    private FrPackersWorkTeamBinding binding;
    private FindPackersAdapter findPackersAdapter;

    //工程分类
    private String firstCategoryNo = "";
    //工程专项
    private PickPopWindow1 popMajor;
    private PickResultEntity pickResultMajor;
    //团队规模
    private PickPopWindow1 popTeamSize;
    private PickResultEntity pickResultTeamSize;
    //所在区域
    private MultiLevelAreaPopWindow popCity;
    private PickResultEntity pickResultCity;
    private PickResultEntity pickResultArea;

    private String third = "", fourth = "", fifth = "", sixth = "";

    private int currentClass = 0;
    private String title;

    public static PackersWorkTeamFragment newInstance(int position) {
        PackersWorkTeamFragment fragment = new PackersWorkTeamFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("TYPE_INDEX", position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerPackersFragmentComponent
                .builder()
                .appComponent(appComponent)
                .packersFragmentModule(new PackersFragmentModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_packers_work_team, container, false);
        return binding.getRoot();
    }

    private void initFilterData(boolean isLoadMore) {
        // <!--0  施工队  1  班组   2承包商-->
        if (currentClass == 1) {
            viewModel.getNewList(pickResultTeamSize.getName(), firstCategoryNo, pickResultMajor.getNo(), third, fourth, fifth, sixth, "0", pickResultCity.getNo(),String.valueOf(currentClass), isLoadMore);
        } else {
            viewModel.getNewList(pickResultTeamSize.getName(), firstCategoryNo, pickResultMajor.getNo(), third, fourth, fifth, sixth, "1", pickResultCity.getNo(),String.valueOf(currentClass), isLoadMore);
        }
    }

    @Override
    protected void initView() {
        currentClass = getArguments().getInt("TYPE_INDEX", 0);
        binding.setViewModel(viewModel);
        binding.tvMajor.setOnClickListener(this);
        binding.tvTeamSize.setOnClickListener(this);
        binding.tvArea.setOnClickListener(this);

        if (findPackersAdapter == null) {
            findPackersAdapter = new FindPackersAdapter(getActivity(), null, Glide.with(this));
        }
        findPackersAdapter.setOnItemClick(new AdapterOnItemClick<FindPackersEntity>() {
            @Override
            public void onItemClick(FindPackersEntity findPackersEntity, int position) {
               // if (findPackersEntity.getIsCompany() == 0) {
                if (currentClass==1){//班组工队
                    title = "班组工队";
                }else {//施工工队
                    title = "施工队";
                }
                    PersonShopActivity.start(getActivity(), findPackersEntity.getNo(), findPackersEntity.getUserNo(),findPackersEntity.getCreateTime(),title);
              /*  } else {
                    ShopInfoActivity.start(getActivity(), findPackersEntity.getCompanyId(), 4);
                }*/
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getResources().getDimension(R.dimen.dp5)));
        binding.recyclerView.setAdapter(findPackersAdapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {
                initFilterData(true);
            }
        });

        pickResultMajor = new PickResultEntity();
        pickResultTeamSize = new PickResultEntity();
        pickResultCity = new PickResultEntity();
        pickResultArea = new PickResultEntity();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_major:
                viewModel.getSecondCategory(firstCategoryNo);
                break;
            case R.id.tv_team_size:
                if (popTeamSize == null){
                    viewModel.getGm(currentClass);
                }else {
                    showGmPopWindow(null);
                }
                break;
            case R.id.tv_area:
                if (popCity == null){
                    viewModel.getLocateCityList();
                }else {
                    showCityPop(null);
                }
                break;
        }
    }

    @Override
    protected void onLazyLoad() {
        //工程分类 一级分类
        viewModel.getProTypeList();
    }

    public void showList(List<FindPackersEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                findPackersAdapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                findPackersAdapter.setDatas(list);
            } else {
                findPackersAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    //工程类型
    public void setProData(List<PickSelectEntity> data) {
        if (data != null && data.size() > 0) {
            firstCategoryNo = data.get(0).getNo();
        }
        binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i = 0; i < data.size(); i++) {
            binding.tabLayout.addTab(binding.tabLayout.newTab().setCustomView(R.layout.item_tablayout_view_2));
            TextView tvTab = binding.tabLayout.getTabAt(i).getCustomView().findViewById(R.id.tv_tab_item);
            tvTab.setTextColor(ContextCompat.getColor(getActivity(), R.color.color_666666));
            tvTab.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tvTab.setText(data.get(i).getProjectscale());
            if (i == 0) {
                tvTab.setTextColor(ContextCompat.getColor(getContext(), R.color.new_blue));
                tvTab.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            }
        }
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView tv = tab.getCustomView().findViewById(R.id.tv_tab_item);
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗
                tv.setTextColor(ContextCompat.getColor(getContext(), R.color.new_blue));
                tv.invalidate();
                firstCategoryNo = data.get(tab.getPosition()).getNo();
                pickResultMajor.clear();
                closeFilterData();
                binding.tvMajor.setText("工程专项");
                setTextViewChange(binding.tvMajor, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                initFilterData(false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView tv = tab.getCustomView().findViewById(R.id.tv_tab_item);
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                tv.setTextColor(ContextCompat.getColor(getContext(), R.color.color_666666));
                tv.invalidate();
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        //列表
        initFilterData(false);
    }


    public void setFilterContent(PickResultEntity first, PickResultEntity second, String third, String fourth, String fifth, String sixth) {
        if (first.getPosition() != -1) {
            firstCategoryNo = first.getNo();
            binding.tabLayout.setScrollPosition(first.getPosition(), 0f, true);
            pickResultMajor.replace(second);
            if (!"".equals(second.getName())) {
                if ("全部".equals(second.getName())) {
                    binding.tvMajor.setText("工程专项");
                    setTextViewChange(binding.tvMajor, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                } else {
                    binding.tvMajor.setText(second.getName());
                    setTextViewChange(binding.tvMajor, R.mipmap.ic_down_arrow, R.color.new_blue);
                }
            } else {
                binding.tvMajor.setText("工程专项");
                setTextViewChange(binding.tvMajor, R.mipmap.ic_down_arrow_n, R.color.color_666666);
            }
            this.third = third;
            this.fourth = fourth;
            this.fifth = fifth;
            this.sixth = sixth;
            initFilterData(false);
        }
    }

    private void closeFilterData() {
        this.third = "";
        this.fourth = "";
        this.fifth = "";
        this.sixth = "";
    }

    //工程专项pop
    public void showMajorPopWindow(List<PickSelectEntity> data) {
        if (popMajor == null) {
            popMajor = new PickPopWindow1(getContext());
            popMajor.setBgView(binding.grayLayout);
            popMajor.setOnResultClick(new PickPopWindow1.OnResultClick() {
                @Override
                public void result(Object key1, Object key2, Object key3) {
                    pickResultMajor.setNo((String) key1);
                    pickResultMajor.setName((String) key2);
                    pickResultMajor.setPosition((int) key3);
                    if (TextUtils.isEmpty((String) key2) || TextUtils.equals("全部", (String) key2)) {
                        binding.tvMajor.setText("工程专项");
                    } else {
                        binding.tvMajor.setText((String) key2);
                    }
                    initFilterData(false);
                }
            });
            popMajor.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    if (TextUtils.isEmpty(pickResultMajor.getName()) || TextUtils.equals("全部", pickResultMajor.getName())) {
                        setTextViewChange(binding.tvMajor, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                    } else {
                        setTextViewChange(binding.tvMajor, R.mipmap.ic_down_arrow, R.color.new_blue);
                    }
                }
            });
        }
        if (data != null && data.size() > 0) {
            popMajor.setData(data);
            popMajor.setDefItem(0);
            setTextViewChange(binding.tvMajor, R.mipmap.ic_up_arrow, R.color.new_blue);
            popMajor.showPopwindow0(binding.clLayout);
        }
    }

    //团队规模pop
    public void showGmPopWindow(List<PickSelectEntity> data) {
        if (popTeamSize == null) {
            popTeamSize = new PickPopWindow1(getContext());
            popTeamSize.setBgView(binding.grayLayout);
            popTeamSize.setOnResultClick(new PickPopWindow1.OnResultClick() {
                @Override
                public void result(Object key1, Object key2, Object key3) {
                    pickResultTeamSize.setNo((String) key1);
                    pickResultTeamSize.setName((String) key2);
                    pickResultTeamSize.setPosition((int) key3);
                    if (TextUtils.isEmpty((String) key2) || TextUtils.equals("全部", (String) key2)) {
                        binding.tvTeamSize.setText("团队规模");
                    } else {
                        binding.tvTeamSize.setText((String) key2);
                    }
                    initFilterData(false);
                }
            });
            popTeamSize.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    if (TextUtils.isEmpty(pickResultTeamSize.getName()) || TextUtils.equals("全部", pickResultTeamSize.getName())) {
                        setTextViewChange(binding.tvTeamSize, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                    } else {
                        setTextViewChange(binding.tvTeamSize, R.mipmap.ic_down_arrow, R.color.new_blue);
                    }
                }
            });

            popTeamSize.setData(data);
        }
        setTextViewChange(binding.tvTeamSize, R.mipmap.ic_up_arrow, R.color.new_blue);
        popTeamSize.showPopwindow0(binding.clLayout);
    }

    //市、区
    public void showCityPop(List<BaseAreaEntity<List<BaseAreaEntity>>> data) {
        if (popCity == null) {
            popCity = new MultiLevelAreaPopWindow(getContext());
            popCity.setBgView(binding.grayLayout);
            popCity.setOnResultClick(new BasePopWindow.OnResultClick() {
                @Override
                public void result(Object key1, Object key2, Object key3) {
                    BaseAreaEntity baseAreaEntityCity = (BaseAreaEntity) key2;
                    if (baseAreaEntityCity != null){
                        pickResultCity.setNo(baseAreaEntityCity.getNo());
                        pickResultCity.setName(baseAreaEntityCity.getName());
                        if (TextUtils.isEmpty(baseAreaEntityCity.getNo()) || TextUtils.equals("全部", baseAreaEntityCity.getName())) {
                            binding.tvArea.setText("所在地区");
                        } else {
                            binding.tvArea.setText(baseAreaEntityCity.getName());
                        }
                        initFilterData(false);
                    }
                   /* List<BaseAreaEntity> baseAreaEntities = (List<BaseAreaEntity>) key3;
                    if (baseAreaEntities != null) {
                        for (BaseAreaEntity selected : baseAreaEntities) {
                            pickResultArea.setNo(selected.getNo());
                            pickResultArea.setName(selected.getName());
                            if (TextUtils.isEmpty(selected.getNo()) || TextUtils.equals("全部", selected.getName())) {
                                binding.tvArea.setText("所在地区");
                            } else {
                                binding.tvArea.setText(selected.getName());
                            }
                            initFilterData(false);
                        }
                    }*/
                }
            });
            popCity.setListener(new MultiLevelAreaPopWindow.ReSetClickListener() {
                @Override
                public void reSet() {
                    binding.tvArea.setText("所在地区");
                    pickResultCity.clear();
                    initFilterData(false);
                }
            });
            popCity.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    if (TextUtils.isEmpty(pickResultCity.getNo())) {
                        setTextViewChange(binding.tvArea, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                    } else {
                        setTextViewChange(binding.tvArea, R.mipmap.ic_down_arrow, R.color.new_blue);
                    }
                }
            });

            BaseAreaEntity baseAreaEntity = new BaseAreaEntity();
            baseAreaEntity.setName("全部");
            baseAreaEntity.setNo("");
            baseAreaEntity.setChildren(new ArrayList<>());
            for (int i = 0; i < data.size(); i++) {
                data.get(i).getChildren().add(0, baseAreaEntity);
            }
            popCity.setCityList(data);
            popCity.setCitySelectType(true);
            popCity.setAreaSelectType(false, true);
        }
        setTextViewChange(binding.tvArea, R.mipmap.ic_up_arrow, R.color.new_blue);
        popCity.showPopwindow0(binding.clLayout);
    }

    private void setTextViewChange(TextView textView, int img, int textColor) {
        Resources res = getResources();
        Drawable drawable = res.getDrawable(img);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable, null);
        textView.setTextColor(getResources().getColor(textColor));
    }

}
